package com.zhiyizhen.handheld.common;

import android.content.Context;
import android.view.View;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.zhiyizhen.handheld.common.view.LoadMoreLayout;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

public class RecyclerViewUtil {
    private RecyclerViewUtil() {
    }

    /**
     * 线性纵向RecyclerView
     */
    public static void configLVRecyclerView(Context context, XRecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        configRecyclerView(context, recyclerView, layoutManager);
    }

    /**
     * 配置RecyclerView
     */
    public static void configRecyclerView(Context context, XRecyclerView recyclerView, RecyclerView.LayoutManager layoutManager) {
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setRefreshProgressStyle(ProgressStyle.BallBeat);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        LoadMoreLayout footView = new LoadMoreLayout(context);
        recyclerView.setFootView(footView, footView);
    }

    /**
     * 加载完成
     *
     * @param recyclerView XRecyclerView
     * @param page 页数
     * @param success 是否加载成功，如果成功，则更新加载更多View，判断是否有更多。
     * @param noMore 是否还有更多
     * @param isEmpty 数据是否为空
     */
    public static void loadComplete(XRecyclerView recyclerView, int page, boolean success, boolean noMore, boolean isEmpty) {
        if (recyclerView == null) {
            return;
        }
        if (page == 1) {
            recyclerView.refreshComplete();
        } else {
            recyclerView.loadMoreComplete();
        }

        if (!success) {
            return;
        }
        recyclerView.setNoMore(noMore);

        if (noMore && isEmpty) {
            View footView = recyclerView.getFootView();
            if (!(footView instanceof LoadMoreLayout)) {
                return;
            }
            ((LoadMoreLayout) footView).showEmptyView();
        }
    }

    public static void setEmptyView(XRecyclerView recyclerView, @DrawableRes int iconId, @StringRes int textId) {
        if (recyclerView == null) {
            return;
        }

        View footView = recyclerView.getFootView();
        if (!(footView instanceof LoadMoreLayout)) {
            return;
        }
        ((LoadMoreLayout) footView).setEmptyView(iconId, textId);
    }

    /**
     * 释放资源
     */
    public static void destroy(XRecyclerView recyclerView) {
        if (recyclerView == null) {
            return;
        }
        recyclerView.destroy();

        View footView = recyclerView.getFootView();
        if (!(footView instanceof LoadMoreLayout)) {
            return;
        }
        ((LoadMoreLayout) footView).destroy();
    }

    /**
     * 获取RecyclerView 滑动的距离
     * 使用方法：layoutManager.scrollToPositionWithOffset(position[0], position[1]);
     *
     * @return pos[0] 当前item的position；pos[1] 当前项的偏移量，
     */
    public static int[] getRecyclerViewLastPosition(RecyclerView mRecyclerView, BaseAdapter mAdapter, LinearLayoutManager layoutManager) {
        int[] pos = new int[2];
        pos[0] = layoutManager.findFirstCompletelyVisibleItemPosition();
        OrientationHelper orientationHelper = OrientationHelper.createOrientationHelper(layoutManager, RecyclerView.VERTICAL);
        int fromIndex = 0;
        int toIndex = mAdapter.getData().size();
        final int start = orientationHelper.getStartAfterPadding();
        final int end = orientationHelper.getEndAfterPadding();
        final int next = toIndex > fromIndex ? 1 : -1;

        View child;
        int childStart;
        int childEnd;
        for (int i = fromIndex; i != toIndex; i += next) {
            child = mRecyclerView.getChildAt(i);
            childStart = orientationHelper.getDecoratedStart(child);
            childEnd = orientationHelper.getDecoratedEnd(child);
            if (childStart >= start && childStart < end && childEnd > start && childEnd <= end) {
                pos[1] = childStart;
                return pos;
            }
        }
        return pos;
    }

}
